package com.ruoyi.system.utils;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.vo.SysUserVO;
import com.ruoyi.common.exception.UtilException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.system.domain.ClockRule;
import com.ruoyi.system.domain.SysLeave;
import com.ruoyi.system.domain.SysPersonnelClock;
import com.ruoyi.system.service.IClockRuleService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * 包名称： com.ruoyi.system.utils
 * 类名称： ClockListUtils
 * 类描述： 打卡集合工具类
 * 创建人： @author tanglei
 * 创建时间： 2021/1/26/11:42
 */
public class ClockUtils {

    //一天的毫秒数
    public static final long ONE_DAY_MSEC = 1000*24*60*60L;
    //一小时的毫秒数
    public static  final long ONE_HOUR_MSEC = 1000*60*60L;

    public static int setClockState(SysPersonnelClock clock, ClockRule clockRule){
        //旷工时间
        Long absenceTime = timeStrToLong(clockRule.getAbsenceTime());
        //迟到时间
        Long lateTime = timeStrToLong(clockRule.getLateTime());
        //早退时间
        Long EarlyLeaveTime = timeStrToLong(clockRule.getEarlyLeaveTime());
        //获取打卡时间数组
        String[] clockTimesArr = clock.getClockTime().split(", ");
        //转换为long类型数组
        Long[] timeForLongType = ClockUtils.strArrToLong(clockTimesArr);
        int length = timeForLongType.length;
        //1、判断是否为工作日星期
        //2、判断是否是在假期中
        //3、判断打卡状态
        if (length < 1 || timeForLongType[0] > absenceTime ){
            //长度小于0、首次打卡时间在旷工时间之后、未打卡，显示旷工
            return 3;
        }else if (length>= 2 && timeForLongType[length-1] < EarlyLeaveTime){
            //长度大于2、末尾次打卡在早退时间前，显示早退
            return 2;
        }else if (length>= 2 && timeForLongType[0] > lateTime){
            //迟到
            return 1;
        }else if (length >= 2 && timeForLongType[0] < lateTime && timeForLongType[length-1]>= EarlyLeaveTime){
            //正常
            return 0;
        }
        return 0;
    }

    /**
     * 将字符串类型数组转化为long类型数组
     * @param timeStrArr 字符串时间数组
     * @return long类型时间数组
     */
    public static Long[] strArrToLong(String[] timeStrArr){
        Long[] longTimeArr = new Long[timeStrArr.length];
        for (int i = 0; i < timeStrArr.length; i++) {
            longTimeArr[i] = ClockUtils.timeStrToLong(timeStrArr[i]);
        }
        return longTimeArr;
    }

    /**
     * 将字符串日期转换为long类型
     * @param timeStr 字符串日期
     * @return long 类型时间
     */
    public static Long timeStrToLong(String timeStr){
        return Long.valueOf(timeStr.trim().replaceAll("\\:",""));
    }

    /**
     * 获取注入个人id和租户id的打卡对象
     * @return SysPersonnelClock
     */
    public static SysPersonnelClock getPeronnelClockBean(){
        SysUser sysUser = ShiroUtils.getSysUser();
        SysPersonnelClock personnelClock = new SysPersonnelClock();
        personnelClock.setPersonneId(sysUser.getUserId().intValue());
        personnelClock.setTenantId(sysUser.getTenantId());
        return personnelClock;
    }

    /**
     * 计算出打卡出勤时间
     * @return 打卡出勤时间
     */
    public static String getClockTimes(SysPersonnelClock sysPersonnelClock) throws Exception {
        Integer numberOdClocks = sysPersonnelClock.getNumberOdClocks();
        if (sysPersonnelClock != null){
            if("".equals(sysPersonnelClock.getClockTime()) || sysPersonnelClock.getClockTime() == null){
                return  "0.00";
            }else if (sysPersonnelClock.getNumberOdClocks() == 1 || sysPersonnelClock.getNumberOdClocks() == 0){
                return  "0.00";
            }
            String[] clockTimesArr = new String[numberOdClocks];
            clockTimesArr = sysPersonnelClock.getClockTime().replace(" ","").split(",");
            if (clockTimesArr.length >= 2){
                return dateDiff(clockTimesArr[0], clockTimesArr[clockTimesArr.length-1], "HH:mm:ss");
            }
        }
        return "0.00";
    }


    /**
     *
     * @param sysPersonnelClock 打卡对象
     * @param flag 获取的打卡时间标识【first，last】
     * @return
     */
    public static String getClockTimeStr(SysPersonnelClock sysPersonnelClock, String flag){
        String[] ClockTimeArr = null;
        if (sysPersonnelClock != null && sysPersonnelClock.getNumberOdClocks() != null){
            ClockTimeArr = new String[sysPersonnelClock.getNumberOdClocks()];
            ClockTimeArr = sysPersonnelClock.getClockTime().split(", ");
            //如果获取的是首次打卡
            if ("first".equals(flag)){
                return ClockTimeArr[0];
            }else if ("last".equals(flag)){
                return ClockTimeArr[ClockTimeArr.length-1];
            }
        }
        return "00:00:00";
    }

    /**
     * 计算两个字符串时间相差小时数
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @param format 格式化模板
     * @return 小时数
     * @throws Exception
     */
    public static String dateDiff(String startTime, String endTime, String format) throws Exception {
        SimpleDateFormat sd = new SimpleDateFormat(format);
        //计算两个时间的毫秒时间差异
        long diff = sd.parse(endTime).getTime() - sd.parse(startTime).getTime();
        DecimalFormat df = new DecimalFormat("0.00");
        float clockTimes = ((float)diff % ONE_DAY_MSEC / ONE_HOUR_MSEC) - 1;
        return df.format(clockTimes);
    }


    /**
     * 获取所有假期时间
     * @param clockRule 规则
     * @return 假期
     */
    public static List<String> getAllFreeDays(ClockRule clockRule) {
        List<String> freeDays = new ArrayList<>(16);
        String[] newHolidayArr = clockRule.getNewYearHolidays().split(" - ");
        String[] SpringFestivalHolidaysArr = clockRule.getSpringFestivalHolidays().split(" - ");
        String[] QingmingHolidaysArr = clockRule.getQingmingHolidays().split(" - ");
        String[] FiveOneHolidaysArr = clockRule.getFiveOneHolidays().split(" - ");
        String[] DuanWuHolidaysArr = clockRule.getDuanWuHolidays().split(" - ");
        String[] ZhongQiuHolidaysArr = clockRule.getZhongQiuHolidays().split(" - ");
        String[] GuoQinHolidaysArr = clockRule.getGuoQinHolidays().split(" - ");
        String[] OtherHolidaysArr = clockRule.getOtherHolidays().split(" - ");
        try {
            List<String> newHoliday = DateUtils.getBetweenDays(newHolidayArr[0], newHolidayArr[1]);
            List<String> SpringFestivalHolidays = DateUtils.getBetweenDays(SpringFestivalHolidaysArr[0], SpringFestivalHolidaysArr[1]);
            List<String> QingmingHolidays = DateUtils.getBetweenDays(QingmingHolidaysArr[0], QingmingHolidaysArr[1]);
            List<String> FiveOneHolidays = DateUtils.getBetweenDays(FiveOneHolidaysArr[0], FiveOneHolidaysArr[1]);
            List<String> DuanWuHolidays = DateUtils.getBetweenDays(DuanWuHolidaysArr[0], DuanWuHolidaysArr[1]);
            List<String> ZhongQiuHolidays = DateUtils.getBetweenDays(ZhongQiuHolidaysArr[0], ZhongQiuHolidaysArr[1]);
            List<String> GuoQinHolidays = DateUtils.getBetweenDays(GuoQinHolidaysArr[0], GuoQinHolidaysArr[1]);
            List<String> OtherHolidays = DateUtils.getBetweenDays(OtherHolidaysArr[0], OtherHolidaysArr[1]);
            freeDays.addAll(newHoliday);
            freeDays.addAll(SpringFestivalHolidays);
            freeDays.addAll(QingmingHolidays);
            freeDays.addAll(DuanWuHolidays);
            freeDays.addAll(ZhongQiuHolidays);
            freeDays.addAll(GuoQinHolidays);
            freeDays.addAll(FiveOneHolidays);
            freeDays.addAll(OtherHolidays);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return freeDays;
    }

    /**
     * 获取休息的星期
     * @param clockRule
     * @return
     */
    public static List<String> getNotWorkWeeks(ClockRule clockRule){
        ArrayList<String> notWorkWeeks = new ArrayList<>(16);
        if (clockRule != null){
            if ("1".equals(clockRule.getMonday())){
                notWorkWeeks.add("星期一");
            }
            if ("1".equals(clockRule.getTuesday())){
                notWorkWeeks.add("星期二");
            }
            if ("1".equals(clockRule.getWednesday())){
                notWorkWeeks.add("星期三");
            }
            if ("1".equals(clockRule.getThursday())){
                notWorkWeeks.add("星期四");
            }
            if ("1".equals(clockRule.getFriday())){
                notWorkWeeks.add("星期五");
            }
            if ("1".equals(clockRule.getMonday())){
                notWorkWeeks.add("星期六");
            }
            if ("1".equals(clockRule.getMonday())){
                notWorkWeeks.add("星期七");
            }
        }
        return notWorkWeeks;
    }



    public static void main(String[] args) {

    }
}
